In [1]:
import tensorflow as tf
from tensorflow.keras import layers, models,preprocessing,regularizers,callbacks
from matplotlib import pyplot as plt
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D,MaxPool2D,Dropout,Flatten,Dense,BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.callbacks import EarlyStopping
In [3]:
import numpy as np
sample_image = image.load_img("E:/Depression_project/Balanced data/test/nondepressed/1P307_no_silence.jpeg")
print(sample_image)
np.array(sample_image)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at 0x22A6248E898>
Out[3]:
array([[[  2,   0, 190],
        [  1,   0, 167],
        [  0,   7,   0],
        ...,
        [  6,   3, 242],
        [  0,  20, 108],
        [  3,  32, 255]],

       [[  0,   0, 124],
        [  0,   0, 103],
        [  0,   4,   4],
        ...,
        [  1,   2, 108],
        [  0,   3, 101],
        [  1,   0, 234]],

       [[  0,   0, 134],
        [  0,   0, 142],
        [  0,   6,   2],
        ...,
        [  0,   0, 101],
        [  0,   0, 115],
        [ 16,   6, 255]],

       ...,

       [[  0, 162, 217],
        [ 99, 254, 188],
        [  0, 156, 255],
        ...,
        [ 75, 233, 182],
        [ 92, 216, 255],
        [ 55, 237, 176]],

       [[  0, 234, 255],
        [ 23, 111, 237],
        [ 16, 187, 239],
        ...,
        [  5,  59, 255],
        [  9,  38, 226],
        [  0,  74, 248]],

       [[  0, 234, 237],
        [  0, 107, 255],
        [ 15, 207, 218],
        ...,
        [ 13,  43, 255],
        [  0,  25, 255],
        [  3,  81, 231]]], dtype=uint8)
In [6]:
datagen = preprocessing.image.ImageDataGenerator(rescale=1/255)# normalisation
ts=(512,512)

train_generator = datagen.flow_from_directory(
        'E:/Depression_project/Balanced data/train',
        batch_size=10,
        target_size=ts,
        class_mode='binary')

val_generator = datagen.flow_from_directory(
        'E:/Depression_project/Balanced data/val',
        color_mode='rgb',
        target_size=ts,
        class_mode='binary')

test_generator = datagen.flow_from_directory(
        'E:/Depression_project/Balanced data/test',
        color_mode='rgb',
        target_size=ts,
        class_mode='binary')
Found 1136 images belonging to 2 classes.
Found 134 images belonging to 2 classes.
Found 68 images belonging to 2 classes.
In [7]:
# model
input_shape=(512, 512, 3)

model = models.Sequential()

model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=input_shape))
model.add(layers.MaxPooling2D((2,2), strides=2))

model.add(layers.Conv2D(128, (3, 3), activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(layers.MaxPooling2D(2,2))

model.add(layers.Flatten())

model.add(layers.Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(layers.Dropout(0.5))

model.add(layers.Dense(32, activation='relu',kernel_regularizer=regularizers.l2(0.0001)))
model.add(layers.Dropout(0.5))

model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer=tf.keras.optimizers.SGD(),
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=['accuracy',
                       tf.keras.metrics.TrueNegatives(),
                       tf.keras.metrics.TruePositives(),
                       tf.keras.metrics.FalseNegatives(),
                       tf.keras.metrics.FalsePositives()])
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 510, 510, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 255, 255, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 253, 253, 128)     36992     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 126, 126, 128)     0         
_________________________________________________________________
flatten (Flatten)            (None, 2032128)           0         
_________________________________________________________________
dense (Dense)                (None, 128)               260112512 
_________________________________________________________________
dropout (Dropout)            (None, 128)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                4128      
_________________________________________________________________
dropout_1 (Dropout)          (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33        
=================================================================
Total params: 260,154,561
Trainable params: 260,154,561
Non-trainable params: 0
_________________________________________________________________
In [9]:
# fitiing

history=model.fit_generator(train_generator,
                            epochs=10,
                            validation_data=val_generator,
                            shuffle=True,
                            callbacks=[callbacks.EarlyStopping(monitor='val_acc',
                                                               patience=5,
                                                               restore_best_weights=True)])

# using early stopping method to train the model to achieve maximum accuracy
WARNING:tensorflow:From <ipython-input-9-0df55782be81>:9: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/10
114/114 [==============================] - ETA: 0s - loss: 3.7331 - accuracy: 0.5194 - true_negatives: 334.0000 - true_positives: 256.0000 - false_negatives: 312.0000 - false_positives: 234.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 525s 5s/step - loss: 3.7331 - accuracy: 0.5194 - true_negatives: 334.0000 - true_positives: 256.0000 - false_negatives: 312.0000 - false_positives: 234.0000 - val_loss: 3.6282 - val_accuracy: 0.4851 - val_true_negatives: 64.0000 - val_true_positives: 1.0000 - val_false_negatives: 66.0000 - val_false_positives: 3.0000
Epoch 2/10
114/114 [==============================] - ETA: 0s - loss: 3.5640 - accuracy: 0.5114 - true_negatives: 199.0000 - true_positives: 382.0000 - false_negatives: 186.0000 - false_positives: 369.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 533s 5s/step - loss: 3.5640 - accuracy: 0.5114 - true_negatives: 199.0000 - true_positives: 382.0000 - false_negatives: 186.0000 - false_positives: 369.0000 - val_loss: 3.4959 - val_accuracy: 0.5000 - val_true_negatives: 0.0000e+00 - val_true_positives: 67.0000 - val_false_negatives: 0.0000e+00 - val_false_positives: 67.0000
Epoch 3/10
114/114 [==============================] - ETA: 0s - loss: 3.4313 - accuracy: 0.5449 - true_negatives: 242.0000 - true_positives: 377.0000 - false_negatives: 191.0000 - false_positives: 326.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 532s 5s/step - loss: 3.4313 - accuracy: 0.5449 - true_negatives: 242.0000 - true_positives: 377.0000 - false_negatives: 191.0000 - false_positives: 326.0000 - val_loss: 3.3733 - val_accuracy: 0.5000 - val_true_negatives: 67.0000 - val_true_positives: 0.0000e+00 - val_false_negatives: 67.0000 - val_false_positives: 0.0000e+00
Epoch 4/10
114/114 [==============================] - ETA: 0s - loss: 3.3082 - accuracy: 0.5555 - true_negatives: 280.0000 - true_positives: 351.0000 - false_negatives: 217.0000 - false_positives: 288.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 513s 5s/step - loss: 3.3082 - accuracy: 0.5555 - true_negatives: 280.0000 - true_positives: 351.0000 - false_negatives: 217.0000 - false_positives: 288.0000 - val_loss: 3.2494 - val_accuracy: 0.4851 - val_true_negatives: 61.0000 - val_true_positives: 4.0000 - val_false_negatives: 63.0000 - val_false_positives: 6.0000
Epoch 5/10
114/114 [==============================] - ETA: 0s - loss: 3.1832 - accuracy: 0.5722 - true_negatives: 295.0000 - true_positives: 355.0000 - false_negatives: 213.0000 - false_positives: 273.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 517s 5s/step - loss: 3.1832 - accuracy: 0.5722 - true_negatives: 295.0000 - true_positives: 355.0000 - false_negatives: 213.0000 - false_positives: 273.0000 - val_loss: 3.1260 - val_accuracy: 0.6642 - val_true_negatives: 31.0000 - val_true_positives: 58.0000 - val_false_negatives: 9.0000 - val_false_positives: 36.0000
Epoch 6/10
114/114 [==============================] - ETA: 0s - loss: 3.0577 - accuracy: 0.5907 - true_negatives: 343.0000 - true_positives: 328.0000 - false_negatives: 240.0000 - false_positives: 225.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 475s 4s/step - loss: 3.0577 - accuracy: 0.5907 - true_negatives: 343.0000 - true_positives: 328.0000 - false_negatives: 240.0000 - false_positives: 225.0000 - val_loss: 3.0070 - val_accuracy: 0.6418 - val_true_negatives: 55.0000 - val_true_positives: 31.0000 - val_false_negatives: 36.0000 - val_false_positives: 12.0000
Epoch 7/10
114/114 [==============================] - ETA: 0s - loss: 2.9324 - accuracy: 0.6567 - true_negatives: 380.0000 - true_positives: 366.0000 - false_negatives: 202.0000 - false_positives: 188.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 457s 4s/step - loss: 2.9324 - accuracy: 0.6567 - true_negatives: 380.0000 - true_positives: 366.0000 - false_negatives: 202.0000 - false_positives: 188.0000 - val_loss: 2.8964 - val_accuracy: 0.7164 - val_true_negatives: 30.0000 - val_true_positives: 66.0000 - val_false_negatives: 1.0000 - val_false_positives: 37.0000
Epoch 8/10
114/114 [==============================] - ETA: 0s - loss: 2.7953 - accuracy: 0.6981 - true_negatives: 396.0000 - true_positives: 397.0000 - false_negatives: 171.0000 - false_positives: 172.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 463s 4s/step - loss: 2.7953 - accuracy: 0.6981 - true_negatives: 396.0000 - true_positives: 397.0000 - false_negatives: 171.0000 - false_positives: 172.0000 - val_loss: 2.7497 - val_accuracy: 0.8284 - val_true_negatives: 63.0000 - val_true_positives: 48.0000 - val_false_negatives: 19.0000 - val_false_positives: 4.0000
Epoch 9/10
114/114 [==============================] - ETA: 0s - loss: 2.6276 - accuracy: 0.7474 - true_negatives: 409.0000 - true_positives: 440.0000 - false_negatives: 128.0000 - false_positives: 159.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 455s 4s/step - loss: 2.6276 - accuracy: 0.7474 - true_negatives: 409.0000 - true_positives: 440.0000 - false_negatives: 128.0000 - false_positives: 159.0000 - val_loss: 2.5905 - val_accuracy: 0.8955 - val_true_negatives: 57.0000 - val_true_positives: 63.0000 - val_false_negatives: 4.0000 - val_false_positives: 10.0000
Epoch 10/10
114/114 [==============================] - ETA: 0s - loss: 2.4639 - accuracy: 0.8169 - true_negatives: 454.0000 - true_positives: 474.0000 - false_negatives: 94.0000 - false_positives: 114.0000WARNING:tensorflow:Early stopping conditioned on metric `val_acc` which is not available. Available metrics are: loss,accuracy,true_negatives,true_positives,false_negatives,false_positives,val_loss,val_accuracy,val_true_negatives,val_true_positives,val_false_negatives,val_false_positives
114/114 [==============================] - 459s 4s/step - loss: 2.4639 - accuracy: 0.8169 - true_negatives: 454.0000 - true_positives: 474.0000 - false_negatives: 94.0000 - false_positives: 114.0000 - val_loss: 2.4731 - val_accuracy: 0.7463 - val_true_negatives: 64.0000 - val_true_positives: 36.0000 - val_false_negatives: 31.0000 - val_false_positives: 3.0000
In [10]:
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Loss Graph')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
In [11]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Accuracy Graph')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
In [12]:
loss,accuracy,tn,tp,fn,fp=model.evaluate_generator(test_generator)

print("Accuracy:", accuracy)
print("True Negative:", tn)
print("True Positive:", tp)
print("False Negative:", fn)
print("False Positive:", fp)
WARNING:tensorflow:From <ipython-input-12-d57c59728e39>:1: Model.evaluate_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.evaluate, which supports generators.
Accuracy: 0.720588207244873
True Negative: 30.0
True Positive: 19.0
False Negative: 15.0
False Positive: 4.0
In [13]:
train_generator.class_indices
Out[13]:
{'depressed': 0, 'nondepressed': 1}
In [14]:
import os
from IPython.display import Image,display

DATADIR = "E:/Depression_project/Balanced data/test"

CATEGORIES = ["depressed", "nondepressed"]

for category in CATEGORIES:  
    path = os.path.join(DATADIR,category)  
    for img in os.listdir(path): 
        display(Image(filename=os.path.join(path,img), width=200, height=200))
        test_image = image.load_img(os.path.join(path,img), target_size = ts)
        test_image = image.img_to_array(test_image)
        test_image = np.expand_dims(test_image, axis = 0)
        result = model.predict(test_image)
        print("Path of the Spectrogram image:", os.path.join(path,img))
        if result[0][0] == 1:
            prediction = 'This Spectrogram image is of Non-depressed category'
        else:
            prediction = 'This Spectrogram image is of Depressed category'
        print(prediction)    
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P330_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P332_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P338_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P345_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P377_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P412_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\0P441_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\11P405_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P332_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P344_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P344_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P353_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P355_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P362_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P386_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P440_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\1P453_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P325_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P362_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P384_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P413_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P421_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\2P441_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P337_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P359_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P376_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P386_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P414_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\3P418_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\4P352_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\4P380_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\4P440_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\5P421_no_silence - Copy.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\depressed\7P380_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\0P343_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\0P442_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\0P443_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\0P450_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\14P466_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P307_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P310_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P326_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P336_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P399_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P404_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P409_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P430_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\1P485_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P328_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P336_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P430_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P438_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P476_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\2P477_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\3P452_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\3P482_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\4P341_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\4P481_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\5P369_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\6P432_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\6P449_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\6P462_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\7P407_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\7P434_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\7P451_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\8P407_no_silence.jpeg
This Spectrogram image is of Non-depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\8P438_no_silence.jpeg
This Spectrogram image is of Depressed category
Path of the Spectrogram image: E:/Depression_project/Balanced data/test\nondepressed\8P450_no_silence.jpeg
This Spectrogram image is of Depressed category
In [15]:
from sklearn.metrics import classification_report, confusion_matrix

pred = model.predict_generator(test_generator)
pred = np.argmax(pred, axis=1)

print('Below is the Confusion Matrix for our model on test data:')
print(confusion_matrix(test_generator.classes, pred))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print('Below is the Classification Report for our modelon test data:')
print(classification_report(test_generator.classes, pred, target_names=CATEGORIES))
WARNING:tensorflow:From <ipython-input-15-23c8551d59cb>:3: Model.predict_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.predict, which supports generators.
Below is the Confusion Matrix for our model on test data:
[[34  0]
 [34  0]]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Below is the Classification Report for our modelon test data:
              precision    recall  f1-score   support

   depressed       0.50      1.00      0.67        34
nondepressed       0.00      0.00      0.00        34

    accuracy                           0.50        68
   macro avg       0.25      0.50      0.33        68
weighted avg       0.25      0.50      0.33        68

C:\Users\honey jain\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py:1221: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
In [ ]:
tr_generator = datagen.flow_from_directory(
        'E:/Depression_project/Divided_data_copy/train',
        color_mode='rgb',
        target_size=ts,
        class_mode='binary')

train_predictions_baseline = model.predict(tr_generator)
test_predictions_baseline = model.predict(test_generator)
def plot_cm(labels, predictions, p=0.5):
        cm = confusion_matrix(labels, predictions > p)
        plt.figure(figsize=(5,5))
        sns.heatmap(cm, annot=True, fmt="d")
        plt.title('Confusion matrix @{:.2f}'.format(p))
        plt.ylabel('Actual label')
        plt.xlabel('Predicted label')

        print('Legitimate Transactions Detected (True Negatives): ', cm[0][0])
        print('Legitimate Transactions Incorrectly Detected (False Positives): ', cm[0][1])
        print('Fraudulent Transactions Missed (False Negatives): ', cm[1][0])
        print('Fraudulent Transactions Detected (True Positives): ', cm[1][1])
        print('Total Fraudulent Transactions: ', np.sum(cm[1]))
baseline_results = model.evaluate(test_generator)
for name, value in zip(model.metrics_names, baseline_results):
    print(name, ': ', value)
print()

plot_cm(train_predictions_baseline, test_predictions_baseline)
Found 208 images belonging to 2 classes.
In [ ]: